package me.aartikov.flowsample.slice;

import me.aartikov.alligator.util.Log;
import me.aartikov.alligator.Navigator;
import me.aartikov.alligator.helpers.MiFraction;
import me.aartikov.alligator.annotations.RegisterScreen;
import me.aartikov.flowsample.ResourceTable;
import me.aartikov.flowsample.SampleApplication;
import me.aartikov.flowsample.screens.TestSmallScreen;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;


@RegisterScreen(TestSmallScreen.class)
public class TestSmallMiFraction extends MiFraction {
//    Component mRootView;

    Text mCounterTextView;

    Text mForwardButton;

    Text mReplaceButton;

    Text mResetButton;

    Text mBackButton;

    Text mDoubleBackButton;


    private Navigator mNavigator = SampleApplication.getNavigator();

    @Override
    protected Component onComponentAttached(LayoutScatter scatter, ComponentContainer container, Intent intent) {
        Component component = scatter.parse(ResourceTable.Layout_mifraction_small, container, false);
        initComponet(component);

        TestSmallScreen screen = SampleApplication.getScreenResolver().getScreen(this);
        int counter = screen.getCounter();
        mCounterTextView.setText(getFractionAbility().getString(ResourceTable.String_counter_template, counter));
        mForwardButton.setClickedListener(v -> {
            Log.i("TestSmallFragment.onComponentAttached([scatter, container, intent]):45");
            mNavigator.goForward(new TestSmallScreen(counter + 1, ResourceTable.Id_fragment_container, screen.getGroup()));
        });

        mReplaceButton.setClickedListener(v -> mNavigator.replace(new TestSmallScreen(counter, ResourceTable.Id_fragment_container, screen.getGroup())));
        mResetButton.setClickedListener(v -> mNavigator.reset(new TestSmallScreen(1, ResourceTable.Id_fragment_container, screen.getGroup())));
        mBackButton.setClickedListener(v -> mNavigator.goBack());
        mDoubleBackButton.setClickedListener(v -> {
            Log.i("退出 TestSmallFragment.onComponentAttached([scatter, container, intent]):53");
            mNavigator.goBack();
            Log.i("退出 TestSmallFragment.onComponentAttached([scatter, container, intent]):55");
            mNavigator.goBack();
        });

        return component;
    }

    @Override
    protected void setInitialFragmentIfRequired() {

    }

    private void initComponet(Component component) {
//        mRootView = component.findComponentById(ResourceTable.Id_root_view);
        mCounterTextView = (Text) component.findComponentById(ResourceTable.Id_counter_text_view);
        mForwardButton = (Text) component.findComponentById(ResourceTable.Id_forward_button);
        mReplaceButton = (Text) component.findComponentById(ResourceTable.Id_replace_button);
        mResetButton = (Text) component.findComponentById(ResourceTable.Id_reset_button);
        mBackButton = (Text) component.findComponentById(ResourceTable.Id_back_button);
        mDoubleBackButton = (Text) component.findComponentById(ResourceTable.Id_double_back_button);
    }

}
